import bb.cascades 1.0

NavigationPane {
    id: historyNavigation
    peekEnabled: false
    
    Page {
        actions: [
//            // define the actions for first tab here
//            ActionItem {
//                title: qsTr("Rotate")
//                onTriggered: {
//                    //                imgTab1.rotationZ = imgTab1.rotationZ + 90;
//                }
//            },
//            ActionItem {
//                title: qsTr("Break")
//                onTriggered: {
//                    //                imgTab1.imageSource = "asset:///images/picture1br.png";
//                }
//            }
        ]
        
        titleBar: TitleBar {
            id: titleBar
            visibility: ChromeVisibility.Visible
            title: lang.history_title
        }
        
        Container {
            //            Container {
            //                //                background: Color.Blue
            //                
            //                Label {
            //                    text: lang.history_title
            //                }
            //            }
            
            Divider {
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                topPadding: 50
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    verticalAlignment: VerticalAlignment.Center
                    text: lang.history_time_start
                }
                
                DateTimePicker {
                    id: dtp_start
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 3
                    }
                    //                    maxWidth: 500
                }
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                topPadding: 50
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    verticalAlignment: VerticalAlignment.Center
                    text: lang.history_time_end
                }
                
                DateTimePicker {
                    id: dtp_end
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 3
                    }
                    //                    maxWidth: 500
                }
            }
            
            Container {
                layout: StackLayout {
                    orientation: LayoutOrientation.LeftToRight
                }
                topPadding: 50
                
                Label {
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 1
                    }
                    verticalAlignment: VerticalAlignment.Center
                    text: lang.history_type
                }
                
                DropDown {
                    id: dd_type
                    layoutProperties: StackLayoutProperties {
                        spaceQuota: 3
                    }
                    //                    maxWidth: 500
                    
                    selectedOption: option0
                    Option {
                        id: option0
                        text : lang.add_button_expenses
                        //                        description : "Sets the alarm to 1 minutes"
                        value : "expenses"
                    
                    }
                    
                    Option {
                        text : lang.add_button_income
                        //                        description : "Sets the alarm to 2 minutes"
                        value : "income"
                    
                    }
                
                }
            }
            Container {
                topPadding: 50
                horizontalAlignment: HorizontalAlignment.Center
                
                Button {
                    text: lang.history_search
                    
                    onClicked: {
                        var historydetailpage = historyNavigation.getHistoryDetailPage();
                        
                        if (dd_type.selectedValue == "expenses"){
                            var history_rt = database.GetAllPayment(dtp_start.value, dtp_end.value, 0);
                            console.log("expenses history_rt : " + JSON.stringify(history_rt));
                            if (history_rt.length == 0){
                                root.showmsg(lang.error_no_data);
                            }else{
                                historydetailpage.setHistory("expenses", history_rt, dtp_start.value, dtp_end.value);
                                historyNavigation.push(historydetailpage);
                            }
                        }else if (dd_type.selectedValue == "income"){
                            var history_rt = database.GetAllIncome(dtp_start.value, dtp_end.value, 0);
                            console.log("income history_rt : " + JSON.stringify(history_rt));
                            if (history_rt.length == 0){
                                root.showmsg(lang.error_no_data);
                            }else{
                                historydetailpage.setHistory("income", history_rt, dtp_start.value, dtp_end.value);
                                historyNavigation.push(historydetailpage);
                            }
                        }
                        // show detail page when the button is clicked
                        
//                        console.debug("pushing detail " + historydetailpage)
//                        historyNavigation.push(historydetailpage);
                    }
                }
            }
        }
    }
    
    onPopTransitionEnded: {
        home.updateStatus() ;
    }
    
    attachedObjects: [
        ComponentDefinition {
            id: historyDetailDefinition
            source: "HistoryDetail.qml"
        }
    ]
    property Page historyDetailPage
    function getHistoryDetailPage() {
        //        if (! addDetailPage) {
        historyDetailPage = historyDetailDefinition.createObject();
        //        }
        return historyDetailPage;
    }
}